<?php
	$temp = array();
	exec("defaults read com.tegosaurus.appletvdatamaker",$temp);
	array_shift($temp); array_pop($temp);
	$prefs = array();
	foreach($temp as $line){
		$line = substr($line,0,strlen($line)-1);
		list($key,$value) = explode(" = ",trim($line),2);
		$prefs[$key] = $value;
	}
	if(!isset($prefs["posterIMP"])){ $prefs["posterIMP"] = 1; }
	if(!isset($prefs["posterDVD"])){ $prefs["posterDVD"] = 1; }

	$movfile = substr($argv[1], strrpos($argv[1], "/")+1);
	$movdir  = substr($argv[1], 0, strrpos($argv[1], "/"));
	$ext = strtolower(substr($movfile, strrpos($movfile, ".")+1));

	if(file_exists(str_replace($ext,"xml",$argv[1]))){
		$temp = file(str_replace($ext,"xml",$argv[1]));
		foreach($temp as $line){
			$matches = array();
			if(preg_match("/\<title\>(.+)\<\/title\>/",$line,$matches)){
				$movTitle = $matches[1];
			}elseif(preg_match("/\<published\>(.+)\<\/published\>/",$line,$matches)){
				$year = substr($matches[1],0,4);
			}
		}
	}

	if(!isset($movTitle)){ echo '3'; exit; }

$dbg = fopen("/tmp/debugcover.jpg","w");
fwrite($dbg, $argv[1]);


	if(file_exists(str_replace($ext,"jpg",$argv[1]))){
		echo "1"; exit;
	}else{

			$posterdone = false;
			if($prefs["posterIMP"]){
				// Get year's list - from cache or new (cache refresh once a week)
				if(file_exists("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/imp/".$year.".tab")){
					$stat = stat("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/imp/".$year.".tab");
					if($stat["mtime"] > time() - 24*7*3600){
						$file = file("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/imp/".$year.".tab");
						$movieposters = array();
						foreach($file as $line){
							$temp = explode("\t",$line);
							$movieposters[$temp[0]] = explode(",",$temp[1]);
						}
					}
				}
				if(!isset($movieposters[strtolower($movTitle)])){
					$posfile = fopen("/Users/".$_ENV["USER"]."/Library/Caches/com.tegosaurus.appletvdatamaker/imp/".$year.".tab","w");
					$parts = preg_split("/\<tr width \= 200 bgcolor \= \#\d\d\d\d\d\d\>/",implode("",file("http://www.impawards.com/2007/standard.html")));
					array_shift($parts);
	
					$movieposters = array();
					foreach($parts as $movie){
						$lines = explode("\n",$movie);
						$movietitle = trim(str_replace("<td><font size=+1>","",str_replace("</font></td>","",$lines[1])));
						$posters = array();
						preg_match_all("/<A HREF = \"(.+?)\"/",$lines[2],$posters);
						fwrite($posfile,$movietitle."\t".implode(",",$posters[1])."\n");
						$movieposters[strtolower($movietitle)] = $posters[1];
					}
				}

				if(isset($movieposters[strtolower($movTitle)])){
					// Get poster URL
					$file = implode("",file("http://www.impawards.com/2007/".array_shift($movieposters[strtolower($movTitle)])));

					$temp = array();
					preg_match("/<img SRC=\"(.+?)\" ALT = \"Movie Poster Image for /",$file,$temp);
					$poster = "http://www.impawards.com/2007/".$temp[1];
					// Download poster
					$cover = file_get_contents($poster);
					$jpg = fopen(str_replace($ext,"jpg",$argv[1]),"w");
					fwrite($jpg, $cover);
					fclose($jpg);
					$posterdone = true;
					echo '1'; exit;
				}
			}
			
			
			
			if(!$posterdone AND ($prefs["posterDVD"] == 1)){
				$userID = rand(99000000000000,99999999999999);
				$searchURL = "http://www.dvdempire.com/Exec/v5_search_item.asp?userid=".$userID."&site_media_id=&site_id=4&pp=&used=0&search_refined=0&string=";
				$searchpage = file_get_contents($searchURL.urlencode($movTitle));
	
				$itemstartpos = strpos($searchpage, 'item_id=');
				if($itemstartpos){
					$itemendpos = strpos($searchpage, '&', $itemstartpos-1);
					$itempart   = substr($searchpage, $itemstartpos + 8, $itemendpos - $itemstartpos - 8);
					if($itempart != ""){
						$imgurl = "http://images2.dvdempire.com/gen/movies/".$itempart."h.jpg";
						$cover = file_get_contents($imgurl);
						$jpg = fopen(str_replace($ext,"jpg",$argv[1]),"w");
						fwrite($jpg, $cover);
						fclose($jpg);
						echo '1'; exit;
					}
				}
			}


	}
	echo '3';
?>